package forum.repository;

import forum.domain.UserRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/4/1.
 */
public interface UserRoleRepository extends JpaRepository<UserRole, Integer>, JpaSpecificationExecutor<UserRole> {

    @Query(value ="select * from user,role,user_role where user_role.userid=user.userid and user_role.roleid=role.roleid and user.username = ?1", nativeQuery = true)
    List<UserRole> findByUsername(String username);

    @Transactional
    @Modifying
    @Query(value ="delete from user_role where user_role.userid = ?1", nativeQuery = true)
    void deleteByUserid(Integer id);

    @Query(value ="select * from user,role,user_role where user_role.userid=user.userid and user_role.roleid=role.roleid and role.rolename = ?1", nativeQuery = true)
    List<UserRole> findByRolename(String rolename);

}
